<?php
/*********************************************
  Copyright (c) 2011 by DragonflyCMS
  http://dragonflycms.org
  Released under GNU GPL version 2 or any later version

  $Source$
  $Revision$
  $Author$
  $Date$
**********************************************/

abstract class PHP
{

	public static $version;
	public static $sapi;
	public static $run_as;
	public static $can_set;
	public static $error_log;
	public static $safe_mode;

	private static $rollback = array();

	public static function init()
	{
		self::runAs();
		self::$version = (int) preg_replace('#^(\d)\.(\d).*#', '$1$2', phpversion());
		if ($disabled = ini_get('disable_functions')) {
			trigger_error('Host provider disabled the following functions: '. $disabled , E_USER_NOTICE);
		}
		self::$can_set = false === strpos($disabled, 'ini_set');
		if (self::$can_set) {
			self::defaults();
		}
		else if (self::$version < 53) {
			set_magic_quotes_runtime(0);
		}
	}

	# return false on failure, mixed on success
	# also saves config changes to rollback
	public static function set($cfg, $val)
	{
		if (!self::$can_set) return false;
		self::$rollback[] = $cfg;
		return ini_set($cfg, $val);
	}

	public static function get($cfg)
	{
		$val = trim(ini_get($cfg));
		# "", "0", null, "Off"
		if (!$val || 'off' == strtolower($val)) return false;
		# "1", true, "On"
		if (1 == $val || 'on' == strtolower($val)) return true;
		# digits
		if (ctype_digit($val)) return intval($val);
		# strings and floats
		return $val;
	}

	public static function restore()
	{
		while ($cfg = array_shift(self::$rollback)) ini_restore($cfg);
		return true;
	}

	private static function defaults()
	{
		//global $Shutdown;
		//$Shutdown->register(array('PHP', 'restore'));
		// Needs to register to DF on BOOT_DOWN
		$common = array(
			'default_socket_timeout' => 7,
			'display_errors' => DF_MODE_DEVELOPER,
			'display_startup_errors' => DF_MODE_DEVELOPER, // should work in cgi
			'mysql.connect_timeout' => 7,
			'user_agent' => 'DragonflyCMS/'.CPG_NUKE.' (PHP '.PHP_MAJOR_VERSION.'; '.PHP_SAPI.'; '.PHP_OS.')',
			'zlib.output_compression' => 0
		);
		$http = array(
			'max_execution_time' => 30,
			'url_rewriter.tags' => 'nourlrewrite'
		);
		$cli = array(
			'display_errors' => DF_MODE_DEVELOPER ? 'stderr' : 0
		);
		$settings = array_merge($common, ${self::$run_as});
		foreach ($settings as $cfg => $val) {
			self::set($cfg, $val);
		}
		if (!DF_LOG_FILE) {
			self::set('log_errors', 0);
		} else {
			self::set('error_log', DF_LOG_FILE); // it should check if file is writable
			self::set('ignore_repeated_errors', 1);
			self::set('log_errors', 1);
			self::set('log_errors_max_len', 255);
		}
		//$version > 53 ini_set('mail.log', ''); // very cool, also verifing mailing lists?
		if (self::$version < 53) {
			self::set('magic_quotes_runtime', 0);
			self::set('magic_quotes_sybase', 0);
		}
	}

	# only knownledge http and cli, discarge cgi for now
	private static function runAs()
	{
	/* aolserver apache apache2filter apache2handler caudium cgi cgi-fcgi cli continuity
   embed isapi milter nsapi phttpd pi3web roxen thttpd tux webjames */
		if     ( defined('STDIN') && stripos(php_sapi_name(), 'cgi') ) self::$sapi = 'cli-cgi'; //cgi-fcgi
		elseif ( defined('STDIN') )                                    self::$sapi = 'cli';
		elseif ( false !== stripos(php_sapi_name(), 'cgi') )           self::$sapi = 'http-cgi'; //cgi
		else                                                           self::$sapi = 'http';
		self::$run_as = (0 === strpos(self::$sapi, 'cli') && !isset($_SERVER['REQUEST_METHOD'])) ? 'cli' : 'http';
		self::$safe_mode = self::get('safe_mode');
	}

}
PHP::init();
